<?php
// Sankasya Controller
// $Id: SankasyaController.php 4 2009-12-25 09:29:05Z mocapapa@g.pugpug.org $
class SankasyaController extends CController
{
	/**
	 * @var string specifies the default action to be 'list'.
	 */
	public $defaultAction='menu';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
		);
	}

        /**                                                                                                   
         * Declares class-based actions.                                                                      
         */
        public function actions()
        {
          return array(
                       'captcha'=>array(
                                        'class'=>'CaptchaAction',
                                        ),
                       );
        }

	/**
	 * Select a offkai.
	 */
	public function actionMenu()
	{
	        $offkai = new Offkai;
		if(isset($_POST['Offkai']))
		{
			$offkai->attributes=$_POST['Offkai'];
			$offkai->scenario='menu';
			if($offkai->validate())
			  	$this->redirect(array('sankasya/top','offkaicode'=>$offkai->offkaicode));
		}
                $this->render('menu',array('offkai'=>$offkai));

	}

	/**
	 * Shows a particular offkai and select a sankasya
	 * from the sankasya list of an offkai.
	 */
	public function actionTop()
	{
		$offkai = Offkai::model()->findByPk($_GET['offkaicode']);
		if (time() - strtotime($offkai->attributes['kaisaibi']) < 3600*24*7*Sankasya::PROTECT_WEEK) {
			$sankasyaList = $offkai->sankasya;
		} else {
			$sankasya=new Sankasya;
			$sankasya->sankasya = Sankasya::UNKOWN;
			$sankasya->honbun = Sankasya::PROTECT_WEEK.'週間以上前のオフ会参加者の情報は、個人情報保護の観点から非表示としています。';
			$sankasya->modified = '';
			$sankasyaList=array($sankasya);
		}
		$this->render('top',array(
			'offkai'=>$offkai,
			'sankasyaList'=>$sankasyaList,
		));

	}

	/**
	 * Creates a new sankasya.
	 * If creation is successful, the browser will be redirected to the 'show' page.
	 */
	public function actionAdd()
	{
		$sankasya=new Sankasya;
		$sankasya->offkaicode=$_GET['offkaicode'];

		if(isset($_POST['Sankasya']))
		{
			$sankasya->attributes=$_POST['Sankasya'];
			if($sankasya->save())
				$this->redirect(array('sankasya/top','offkaicode'=>$sankasya->offkaicode));
		}
		$this->render('add', array('sankasya'=>$sankasya));
	}

	/**
	 * Updates a particular sankasya.
	 * If update is successful, the browser will be redirected to the 'show' page.
	 */
	public function actionEdit()
	{
		$sankasya=Sankasya::model()->findByPk($_GET['id']);
		$sankasya->dbpassword=$sankasya->password;
		$sankasya->password='';

		if(isset($_POST['Sankasya']))
		{
			$sankasya->attributes=$_POST['Sankasya'];
			if($sankasya->save())
				$this->redirect(array('sankasya/top','offkaicode'=>$sankasya->offkaicode));
		}
		$this->render('edit',array('sankasya'=>$sankasya));
	}

	/**
	 * Deletes a particular sankasya.
	 * If deletion is successful, the browser will be redirected to the 'list' page.
	 */
	public function actionDel()
	{
		$sankasya=Sankasya::model()->findByPk($_GET['id']);
		$sankasya->dbpassword=$sankasya->password;
		$sankasya->password='';

		if(isset($_POST['Sankasya']))
		{
			$sankasya->attributes=$_POST['Sankasya'];
			$offkai->scenario='delete';
			if($sankasya->validate() && $sankasya->delete())
				$this->redirect(array('sankasya/top','offkaicode'=>$sankasya->offkaicode));
		}
		$this->render('del',array('sankasya'=>$sankasya));
	}
}
